﻿using Logistics.Manager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Logistics
{
    public partial class frmSAPExport
    {
        private void new_export(string tc_owr009, string tc_owr002, string tc_owr006, string tc_owr007)
        {
            _directoryPath = Path.Combine(Application.StartupPath, @"LG\WL_WORK\" + tc_owr009);
            if (!Directory.Exists(_directoryPath))
                Directory.CreateDirectory(_directoryPath);
            Logger.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + tc_owr009;
            try
            {
                _new_exportData.new_PrepareData(tc_owr009);
                var fileName = new_BuildOwrData(tc_owr009, tc_owr002, tc_owr006, tc_owr007);
                new_BuildOwwData(tc_owr009, fileName);
                new_BuildOwsData(tc_owr009, fileName);
                new_BuildImaData(tc_owr009, fileName);
                new_BuildOccData(tc_owr009, fileName);
                new_BuildOcc_addData(tc_owr009, fileName);
                _new_exportData.new_EndUpdData(tc_owr009, fileName);
            }
            catch (Exception ex)
            {
                var files = Directory.GetFiles(_directoryPath);
                foreach (var item in files)
                {
                    try
                    {
                        File.Delete(item);
                    }
                    catch { }
                }
                Logger.WriteLine("生成txt文件出错；详细信息：" + ex.ToString());
                MessageBox.Show("生成txt文件出错，请重试！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var _files = Directory.GetFiles(_directoryPath);
            if (_files.Count() > 0)
            {

                UpLoadFile("eric", "topdaily", "ftp://192.168.80.7/", $"WL_TEST/WMS/{tc_owr009}/SEND/", "N");

                UpLoadFile("tiptop", "toperp2011", "ftp://192.168.80.16/", $"/u1/topprod/topcust/edi_test/LG/WL_WORK/{tc_owr009}/BACKUP/SEND/", "N");


                foreach (var item in _files)
                {
                    File.Move(item, item + ".incomplete");
                }
                UpLoadFile("tiptop", "toperp2011", "ftp://192.168.80.16/", $"/u1/topprod/topcust/edi_test/LG/WL_WORK/{tc_owr009}/SEND/", "Y");

                MessageBox.Show("数据处理完毕", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                System.Diagnostics.Process.Start("notepad.exe", Logger.FilePath);
            }
            else
            {
                MessageBox.Show("未生成任何文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 1.新物流_单据传输资料主档
        /// </summary>
        private string new_BuildOwrData(string tc_owr009, string tc_owr002, string tc_owr006, string tc_owr007)
        {
            var result = _new_exportData.new_PrepareOwrData(tc_owr009, tc_owr002, tc_owr006, tc_owr007);
            var local_fileName = $"OWR{DateTime.Now.ToString("yyyyMMddHHmmss")}{tc_owr009}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));

            var fileName = "/u1/topprod/topcust/edi_test/LG/WL_WORK/" + tc_owr009.ToString() + "/SEND/" + local_fileName;
            int i = 0;
            foreach (var item in result)
            {
                var str = $"{item.tc_owr001},{item.tc_owr002},{item.tc_owr003},{item.tc_owr004},{item.tc_owr005 },{item.tc_owr006},{item.tc_owr007},{item.tc_owr008},{item.tc_owr009},{item.tc_owr010},{item.tc_owr0101},{item.tc_owr011},{item.tc_owr012},{item.tc_owr013}/{item.tc_owr0131},{item.tc_owr014},{item.tc_owr015},{item.tc_owr016},{item.tc_owr017 },{item.tc_owr018},{item.tc_owr019}";
                sw.WriteLine(str);
                var owfModel = new Model.TC_OWF_FILE();
                owfModel.TC_OWF001 = fileName;
                owfModel.TC_OWF003 = item.tc_owr002;
                owfModel.TC_OWF004 = item.tc_owr001;
                owfModel.TC_OWF005 = DateTime.Now.ToString("HH:mm:ss");
                owfModel.TC_OWFDATE = DateTime.Now.ToString("yyyy-MM-dd");
                owfModel.TC_OWFUSER = MyForms.User.UserName;
                _new_exportData.new_SaveOwrData(owfModel);
                i = i + 1;
            }
            sw.Flush();
            sw.Close();
            fs.Close();
            if (i == 0) File.Delete(local_filePath);
            return fileName;

        }


        /// <summary>
        /// 2.单据传输资料折扣档
        /// </summary>
        private void new_BuildOwwData(string tc_owr009, string tc_owf001)
        {
            var result = _new_exportData.new_PrepareOwwData(tc_owf001);
            var local_fileName = $"OWW{DateTime.Now.ToString("yyyyMMddHHmmss")}{tc_owr009}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            int i = 0;
            foreach (var item in result)
            {
                var str = $"{item.TC_OWW000},{item.TC_OWW001},{item.TC_OWW002},{item.TC_OWW003},{item.TC_OWW004 }";
                sw.WriteLine(str);
                i = i + 1;
            }
            sw.Flush();
            sw.Close();
            fs.Close();
            if (i == 0) File.Delete(local_filePath);
        }


        /// <summary>
        /// 3.单据传输资料明细档
        /// </summary>
        private void new_BuildOwsData(string tc_owr009, string tc_owf001)
        {
            var result = _new_exportData.new_PrepareOwsData(tc_owf001);
            var local_fileName = $"OWS{DateTime.Now.ToString("yyyyMMddHHmmss")}{tc_owr009}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            int i = 0;
            foreach (var item in result)
            {
                var str = $"{item.TC_OWS000},{item.TC_OWS001},{item.TC_OWS002},{item.TC_OWS003},{item.TC_OWS004 },{item.TC_OWS005},{item.TC_OWS006},{item.TC_OWS007},{item.TC_OWS008 },{item.TC_OWS009},{item.TC_OWS010},{item.TC_OWS011},{item.TC_OWS012},{item.TC_OWS013},{item.TC_OWS014},{item.TC_OWS015},{item.TC_OWS016},{item.TC_OWS017},{item.TC_OWS018},{item.TC_OWS019},{item.TC_OWS020}";
                sw.WriteLine(str);
                i = i + 1;
            }
            sw.Flush();
            sw.Close();
            fs.Close();
            if (i == 0) File.Delete(local_filePath);
        }


        /// <summary>
        /// 4.单据传输资料明细档
        /// </summary>
        private void new_BuildImaData(string tc_owr009, string tc_owf001)
        {
            var result = _new_exportData.new_PrepareImaData(tc_owf001);
            var local_fileName = $"OWM{DateTime.Now.ToString("yyyyMMddHHmmss")}{tc_owr009}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            int i = 0;
            foreach (var item in result)
            {
                //todo  这里sql查询字段和实体字段对不上   ima31_dec，ima25_dec
                var str = $"{item.ima01},{item.ta_ima001},{item.ima02},'',{item.ima31_fac},{item.ima06 },{item.ima31},{item.ima31_dec},{item.ima25},{item.ima25_dec },{item.ima135},{item.ta_ima027},{item.ta_ima028},{item.ta_ima024},{item.ta_ima025},{item.ta_ima026}";
                sw.WriteLine(str);
                i = i + 1;
            }
            sw.Flush();
            sw.Close();
            fs.Close();
            if (i == 0) File.Delete(local_filePath);
        }

        /// <summary>
        /// 5.客户基本资料档
        /// </summary>
        private void new_BuildOccData(string tc_owr009, string tc_owf001)
        {
            var result = _new_exportData.new_PrepareOccData(tc_owf001);
            var local_fileName = $"OWL{DateTime.Now.ToString("yyyyMMddHHmmss")}{tc_owr009}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            int i = 0;
            foreach (var item in result)
            {
                var str = $"{item.tc_owr009},{item.occ01},{item.occ18},{item.ta_occ001},{item.occ041 },,,,{item.ta_occ011a },{item.ta_occ012a},{item.ta_occ006a}";
                sw.WriteLine(str);
                i = i + 1;
            }
            sw.Flush();
            sw.Close();
            fs.Close();
            if (i == 0) File.Delete(local_filePath);
        }


        /// <summary>
        /// 6.客户基本资料档
        /// </summary>
        private void new_BuildOcc_addData(string tc_owr009, string tc_owf001)
        {
            var result = _new_exportData.new_PrepareOcc_addData(tc_owf001);
            var local_fileName = $"OWK{DateTime.Now.ToString("yyyyMMddHHmmss")}{tc_owr009}.txt";
            var local_filePath = Path.Combine(_directoryPath, local_fileName);
            FileStream fs = new FileStream(local_filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            int i = 0;
            foreach (var item in result)
            {
                var str = $"{item.tc_owr009},{item.tc_owr005},{item.occ01},{item.occ242},{item.occ243},{item.occ294 },{item.occ304 },{item.occ296},{item.occ306},{item.occ29},{item.occ30}";
                sw.WriteLine(str);
                i = i + 1;
            }
            sw.Flush();
            sw.Close();
            fs.Close();
            if (i == 0) File.Delete(local_filePath);
        }
    }
}
